<template>
	<view class="homeLayout pageBg">
		<!-- 上面的固定搜索栏 -->
		<custom-nav-bar title="推荐"></custom-nav-bar>


		<!-- 这边是轮播图 -->
		<view class="banner">
			<swiper circular indicator-dots indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff"
				autoplay>
				<swiper-item v-for="item in bannerList" :key="item._id">
					
					<navigator v-if="item.target == 'miniProgram' " 
					 :url="item.url"
					  class="like"
					  target="miniProgram"
					  :app-id="item.appid"
					  >
					<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
					
					
					<navigator v-else :url="`/pages/calsslist/calsslist?${item.url}`" class="like">
					<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
				</swiper-item>

			</swiper>
		</view>
		<!-- 公告栏写在这边 -->
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay interval="1500" duration="300" circular>
					<swiper-item v-for="(item, index) in 4" :key="index">
						<navigator url="/pages/notice/detail">
							母猪产后护理大学一年级教程第一章节课程
						</navigator>

					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="16" color="#33"></uni-icons>
			</view>
		</view>

		<view class="select">
			<common-title>
				<template #name>每日推荐</template>
				<template #custom>
					<view class="date">
						<!-- 直接调用插件 -->
						<uni-icons type="calendar" size="20"></uni-icons>
						<view class="text">
							<!-- 一般用法，copy官方按钮文档那边的 -->
							<uni-dateformat :date="Date.now()" format="yyyy年MM月dd日"></uni-dateformat>
						</view>

					</view>
				</template>
			</common-title>
			<view class="content">
				<!-- scroll-x横向滑动 -->
				<scroll-view scroll-x>
					<view class="box" v-for="item in randomList" @click="goPreview(item._id)">

						<!-- mode="aspectFill"用了不会出现空隙 -->
						<image :src="item.smallPicurl" mode="aspectFill"></image>

					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 专题 下面得做两个容器-->
		<view class="theme">
			<common-title>
				<template #name>专题精选</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>

			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>

		</view>

	</view>
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import { apiGetBanner, apiGetDayRandom, apiGetClassify } from "@/api/apis.js";
	import { onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app"
	const bannerList = ref([]);
	const randomList = ref([]);
	const noticeList = ref([]);
	const classifyList = ref([]);

	/* 网络接口 上面的*/
	const getBanner = async () => {
		/* 直接抽离封装里,并且调用这个接口 */
		let res = await apiGetBanner();
		bannerList.value = res.data

		/* let res =await	uni.request({
				url:"https://tea.qingnian8.com/api/bizhi/homeBanner",
				header:{
					"access-key":"299465"
				}
				
			})
			if(res.data.errCode===0){
				bannerList.value=res.data.data
			} */
		/* console.log("打印的接口数据"+res) */
	}
	/* 中间的 */
	const getDayRandom = async () => {
		let res = await apiGetDayRandom();
		randomList.value = res.data
	}

	//公告的，暂时不用
	/* 	const getNotice = async () =>{
			let res =await	uni.request({
					url:"https://tea.qingnian8.com/api/bizhi/wallNewsList",
					header:{
						"access-key":"299465"
					},
					data:{
						select:true
					}
					
				})
				if(res.data.errCode===0){
					noticeList.value=res.data.data
				}
		} */

	/* 下面的专题精选 */
	const getClassify = async () => {
		let res = await apiGetClassify({
			select: true
		});
		console.log(res);
		classifyList.value = res.data

	}



	/* 点击事件跳转 */
	const goPreview = (id) => {
		/* 写入同步缓存 */
		uni.setStorageSync("storgClassList",randomList.value);
		uni.navigateTo({
			url: "/pages/preview/preview?id="+id

		})
	}
	//分享给好友,这个是分享的函数nShareAppMessage 官方的
	onShareAppMessage((e) => {
		return {
			title: "王同学的壁纸库",
			path: "pages/index/index"
		}	
	})
	//分享朋友圈 需要权限，要不就是用小程序测试号的appid
	//测试号id是wx04394c05eeb40715
	//主号用的是wxd4651c59239cb9b7
	onShareTimeline(() => {
		return {
			title: "王同学壁纸库，好看的手机壁纸",
			imageUrl: "/static/images/logo2.jpg"
		}
	})

	getBanner();
	getDayRandom();
	/* getNotice(); */
	getClassify();
</script>

<style lang="scss" scoped>
	.homeLayout {


		.banner {
			/* 主容器大小样式 */
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				/* 轮播图大小样式 */
				width: 750rpx;
				height: 340rpx;

				/* &-item相当于swiper-item是一种简写方式 继承父级*/
				&-item {
					/* 图片容器继承父级容器百分之百宽高 */
					width: 100%;
					height: 100%;
					padding: 0 30rpx;
					/* 填充内边距 */
					box-sizing: border-box;
					
					.like{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
						/* 内填充 .*/
						image {
							/* 图片继承父级容器百分之百宽高 */
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}

					
				}
			}
		}

		.notice {
			width: 690rpx;
			height: 80rpx;
			background: #f9f9f9;
			margin: 0 auto;
			border-radius: 80rpx;
			display: flex;
			line-height: 80rpx;
			/* 行高 */
			display: flex;

			.left {
				width: 140rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				/* 样式穿透 */
				:deep() {
					.uni-icons {
						/*  !important这个就是把他设为最高优先级去渲染 */
						color: $brand-theme-color !important;

					}

				}

				.text {
					color: $brand-theme-color;
					font-weight: 600;
					font-size: 28rpx;
				}
			}

			.center {
				flex: 1;

				swiper {
					height: 100%;

					/* 中间轮播公告文字的高度 */
					&-item {
						height: 100%;
						font-size: 30rpx;
						color: #666;
						overflow: hidden;
						/* 溢出隐藏 */
						white-space: nowrap;
						/* 不换行 */
						text-overflow: ellipsis;
						/* 显示内容溢出的话会变成..... */
					}
				}
			}

			.right {
				width: 70rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.select {
			padding-top: 50rpx;

			.date {
				color: $brand-theme-color;
				display: flex;
				align-items: center;

				/* 样式穿透 */
				:deep() {
					.uni-icons {
						/*  !important这个就是把他设为最高优先级去渲染 */
						color: $brand-theme-color !important;

					}

				}

				.text {
					margin-left: 5rpx;
				}
			}

			.content {
				width: 720rpx;
				margin-left: 30rpx;
				margin-top: 30rpx;
				/* 标题那边的距离 */

				scroll-view {
					/* 不换行 */
					white-space: nowrap;

					.box {
						width: 200rpx;
						height: 430rpx;
						display: inline-block;
						margin-right: 15rpx;

						image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}

					/* 这个是获取最后一个位置那边的间距,改为30个像素点 */
					.box:last-child {
						margin-right: 30rpx;
					}
				}
			}
		}

		.theme {
			padding: 50rpx 0;

			.more {
				font-size: 32rpx;
				color: #888;
			}

			.content {
				margin-top: 30rpx;
				padding: 0 30rpx;
				display: grid;
				/* 这个也是一种风格,网格式的,对标的是flex */
				gap: 15rpx;
				grid-template-columns: repeat(3, 1fr);
				/* 每行三个 每行都自动分配*/
			}
		}


	}
</style>